package com.newbee.nsq;


import com.github.brainlag.nsq.NSQConsumer;
import com.github.brainlag.nsq.NSQMessage;
import com.github.brainlag.nsq.callbacks.NSQMessageCallback;
import com.github.brainlag.nsq.lookup.DefaultNSQLookup;
import com.github.brainlag.nsq.lookup.NSQLookup;

import java.io.IOException;

/**
 * Created by iss on 17/7/19.
 */
public class Consumer implements NSQMessageCallback {

    public void message(NSQMessage nsqMessage) {
        System.out.println(nsqMessage);
        System.out.println(new String(nsqMessage.getMessage()));
        nsqMessage.finished();
    }

    public static void main(String[] args) throws IOException {
        NSQLookup nsqLookup = new DefaultNSQLookup();
        nsqLookup.addLookupAddress("127.0.0.1", 4161);
        NSQMessageCallback callback = new Consumer();
        NSQConsumer nsqConsumer = new NSQConsumer(nsqLookup, "test","test_channel", callback);

        nsqConsumer.start();
        System.out.println("start");
        System.out.println(nsqConsumer.getTotalMessages());
        int i = System.in.read();

        nsqConsumer.close();
        nsqConsumer.shutdown();
        System.out.println("close");
    }
}
